import {
  Component,
  Injector,
  AfterViewInit,
  ElementRef,
  ViewChild
} from '@angular/core';
import { appModuleAnimation } from '@shared/animations/routerTransition';
import { AppComponentBase } from '@shared/common/app-component-base';
import { CostServiceProxy } from '@shared/service-proxies/service-proxies';
import { NotifyService } from 'abp-ng2-module/dist/src/notify/notify.service';
import { FileDownloadService } from '@shared/utils/file-download.service';
import * as moment from 'moment';
import { finalize } from 'rxjs/operators';

@Component({
  templateUrl: './cost.component.html',
  animations: [appModuleAnimation()]
})
export class CostsComponent extends AppComponentBase {
  @ViewChild('DatePicker') datePicker: ElementRef;

  public startDate: moment.Moment = moment().startOf('day');
  public endDate: moment.Moment = moment().endOf('day');
  saving = false;

  constructor(
    injector: Injector,
    private _costServiceProxy: CostServiceProxy,
    private _notifyService: NotifyService,
    private _fileDownloadService: FileDownloadService
  ) {
    super(injector);
  }

  exportToExcel(type: number): void {
    this.saving = true;

    this._costServiceProxy
      .getCostsToExcel(type, this.startDate, this.endDate)
      .pipe(
        finalize(() => {
          this.saving = false;
        })
      )
      .subscribe(result => {
        this._fileDownloadService.downloadTempFile(result);
      });
  }
}
